package manager.auxiliares;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.Scanner;

public class LerConfiguracoes {

    private final File fFile;

    public class ConfigParam {

        String name;
        String value;

        public ConfigParam(String newName, String newValue) {
            this.name = newName;
            this.value = newValue;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

//  public static void main(String... aArgs) throws FileNotFoundException {
//    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
//    parser.processLineByLine();
//    log("Done.");
//  }
    /**
    Constructor.
    @param aFileName full name of an existing, readable file.
     */
    public LerConfiguracoes(String aFileName) {
        fFile = new File(aFileName);
    }

    /** Template method that calls {@link #processLine(String)}.  */
    public LinkedList<ConfigParam> processLineByLine() throws FileNotFoundException {
        //Note that FileReader is used, not File, since File is not Closeable
        Scanner scanner = new Scanner(new FileReader(fFile));
        LinkedList<ConfigParam> configParamsList = new LinkedList<ConfigParam>();
        ConfigParam configParams;
        try {
            //first use a Scanner to get each line
            while (scanner.hasNextLine()) {
                configParams = processLine(scanner.nextLine());
                if (configParams != null) {
                    configParamsList.add(configParams);
                }
            }
        } finally {
            //ensure the underlying stream is always closed
            //this only has any effect if the item passed to the Scanner
            //constructor implements Closeable (which it does in this case).
            scanner.close();
        }
        return configParamsList;
    }

    /** 
    Overridable method for processing lines in different ways.
    
    <P>This simple default implementation expects simple name-value pairs, separated by an 
    '=' sign. Examples of valid input : 
    <tt>height = 167cm</tt>
    <tt>mass =  65kg</tt>
    <tt>disposition =  "grumpy"</tt>
    <tt>this is the name = this is the value</tt>
     */
    protected ConfigParam processLine(String aLine) {
        //use a second Scanner to parse the content of each line 
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("=");
        if (scanner.hasNext()) {
            String name = scanner.next();
            String value = scanner.next();
            return new ConfigParam(name, value);
        }
        return null;
        //no need to call scanner.close(), since the source is a String
    }
}
